package com.example.apiinvoksdk.utils;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.example.apiinvoksdk.model.dto.GetParams;
import com.example.apiinvoksdk.model.dto.PostParams;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 创建调用客户端
 *
 * @author yanghaifeng
 * @date 2024/1/23 11:11
 */
@Slf4j
@Data
@Component
public class ApiClient {

    private String accessKey;
    private String secretKey;

    public ApiClient() {
    }

    public ApiClient(String accessKey, String secretKey) {
        this.accessKey = accessKey;
        this.secretKey = secretKey;
    }

    /**
     * 接口调用
     *
     * @return
     */
    public Object post(PostParams request) {
        // 请参校验
        String url = request.getUrl();
        String body = request.getParams();
        if (StrUtil.hasBlank(url)) {
            throw new RuntimeException("请求参数有误");
        }
        long timestamp = System.currentTimeMillis();
        System.out.println("时间戳：" + timestamp);

        // 加密，生成请求凭证
        Digester md5 = new Digester(DigestAlgorithm.MD5);
        String encodeStr = body + timestamp + secretKey;
        String digestHex = md5.digestHex(encodeStr);
        log.info("digestHex value:{}", digestHex);

        Map<String, String> header = new HashMap<String, String>();
        header.put("accessKey", accessKey);
        // 将凭证放入请求头
        header.put("certificate", digestHex);
        header.put("timestamp", String.valueOf(timestamp));
        HttpRequest httpRequest = HttpRequest.post(url);
        httpRequest.addHeaders(header);
        if (StrUtil.isNotBlank(body)) {
            httpRequest.body(body);
        }
        HttpResponse response = httpRequest.execute();
        return response.body();

//        HttpResponse response = HttpRequest.post(url)
//                .addHeaders(header)
//                .body(body)
//                .execute();
//        return response.body();
    }


    /**
     * 接口调用
     *
     * @return
     */
    public Object get(GetParams request) {
        // 请参校验
        String url = request.getUrl();
        String queryParams = request.getParams();
        if (StrUtil.hasBlank(url)) {
            throw new RuntimeException("请求参数有误");
        }
        long timestamp = System.currentTimeMillis();
        System.out.println("时间戳：" + timestamp);

        // 加密，生成请求凭证
        Digester md5 = new Digester(DigestAlgorithm.MD5);
        String encodeStr = queryParams + timestamp + secretKey;
        String digestHex = md5.digestHex(encodeStr);
        System.out.println(digestHex);

        Map<String, String> header = new HashMap<String, String>();
        header.put("accessKey", accessKey);
        // 将凭证放入请求头
        header.put("certificate", digestHex);
        header.put("timestamp", String.valueOf(timestamp));
        HttpResponse response = HttpRequest.post(url)
                .addHeaders(header)
                .body(queryParams)
                .execute();
        return response.body();
    }

}
